package lex.fsm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.builder.HashCodeBuilder;

public class State
{
	private final String name;
	private List<Transition> transitions = Collections.emptyList();

	/**
	 * @param name
	 *        the unique id of this {@link State}.
	 */
	public State( String name )
	{
		this.name = name;
		this.transitions = new ArrayList<Transition>( 2 );
	}

	/**
	 * Returns the id of this {@link State}.
	 * 
	 * @return the id.
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * Returns an unmodifiable {@link List} of {@link Transition}s going out
	 * from this {@link State}.
	 * 
	 * @return the {@link Transition}s.
	 */
	public List<Transition> getTransitions()
	{
		return Collections.unmodifiableList( transitions );
	}

	/**
	 * Adds an outgoing {@link Transition} to this {@link State}.
	 * 
	 * @param transition
	 *        the {@link Transition} to add.
	 * @return this {@link State}.
	 */
	public State addTransition( Transition transition )
	{
		if( null != transition )
			this.transitions.add( transition );
		return this;
	}

	@Override
	public boolean equals( Object o )
	{
		if( !(o instanceof State) )
		{
			return false;
		}
		if( o == this )
		{
			return true;
		}
		State that = (State) o;
		return this.name.equals( that.name );
	}

	@Override
	public int hashCode()
	{
		return new HashCodeBuilder( 13, 33 ).append( this.name ).toHashCode();
	}

	@Override
	public String toString()
	{
		return name;
	}
}
